package net.homelinux.chaoswg.io.renga.core.gui.operations;

import javax.swing.JOptionPane;

import net.homelinux.chaoswg.io.renga.core.gui.DrawingArea;
import net.homelinux.chaoswg.io.renga.core.gui.operations.helper.Connection;
import net.homelinux.chaoswg.io.renga.core.gui.operations.visualisations.OutputBlockVisualisation;

import org.jdom.Element;

public class FunctionOutputOperation extends Operation {
    protected CompositeOperation superOperation = null;
    protected int isOutputNumber = -1;

    public FunctionOutputOperation () {
        this.visualisation = new OutputBlockVisualisation (this);

        inputs = 1;
        outputs = 1;

        connections  = new Connection[inputs];
        connections[0] = new Connection();

        visualisation.setInputs(1);
        visualisation.setOutputs(0);
    }

    public FunctionOutputOperation (final CompositeOperation superOperation, final int isOutputNumber)  {
        this.superOperation = superOperation;
        this.visualisation = new OutputBlockVisualisation (this);
        this.isOutputNumber = isOutputNumber;

        inputs = 1;
        outputs = 1;

        connections  = new Connection[inputs];
        connections[0] = new Connection();

        visualisation.setInputs(1);
        visualisation.setOutputs(0);
    }

    public FunctionOutputOperation (final CompositeOperation superOperation)  {
        this.superOperation = superOperation;
        this.visualisation = new OutputBlockVisualisation (this);
        this.isOutputNumber = 0;

        inputs = 1;
        outputs = 1;

        connections  = new Connection[inputs];
        connections[0] = new Connection();

        visualisation.setInputs(1);
        visualisation.setOutputs(0);
    }

    public Object apply (final int i) {
        if (i > 1) {
            return null;
        }

        result = connections[0].getResult();

        return result;
    }

    public String toString () {
        return "Function Output";
    }

    public int getIsOutputNumber() {
        return isOutputNumber;
    }

    public void setIsOutputNumber(final int isOutputNumber) {
        this.isOutputNumber = isOutputNumber;
    }

    @Override
    public String getDoubleClickActionName() {
        return "Nothing";
    }

    @Override
    public void doubleClick(final DrawingArea dv) {
        JOptionPane.showMessageDialog(dv, "Nothing to be done here go away", "Go away", JOptionPane.ERROR_MESSAGE);
    }

    public Element writeYourself () {
        final Element toReturn = super.writeYourself();

        final Element superOperationElement = new Element ("superOperation");
        superOperationElement.setText(""+superOperation.getId());
        toReturn.addContent(superOperationElement);

        final Element isOutputNumberElement = new Element ("outputNumber");
        isOutputNumberElement.setText(""+isOutputNumber);
        toReturn.addContent(isOutputNumberElement);

        return toReturn;
    }

    public CompositeOperation getSuperOperation() {
        return superOperation;
    }

    public void setSuperOperation(final CompositeOperation superOperation) {
        this.superOperation = superOperation;
    }

}
